//
//  THBottomTool.m
//  duolaCard
//
//  Created by 希达 on 2018/5/8.
//  Copyright © 2018年 zyy. All rights reserved.
//

#import "THBottomTool.h"

@interface THBottomTool ()
@property (weak, nonatomic) UIButton *backBtn;
@property (weak, nonatomic) UIButton *doneBtn;
@end

@implementation THBottomTool

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configBtn];
    }
    return self;
}

- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    [self.backBtn setTitle:leftTitle forState:UIControlStateNormal];
}
- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    [self.doneBtn setTitle:rightTitle forState:UIControlStateNormal];
}

- (void)configBtn {
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.tz_width * 0.5 - 50, 5, 100, 34)];
    self.backBtn = backBtn;
    [backBtn setTitle:@"Back" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];

    UIButton *doneBtn = [[UIButton alloc] initWithFrame:backBtn.frame];
    [doneBtn setTitle:@"Done" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    doneBtn.hidden = true;
    [self addSubview:doneBtn];
    self.doneBtn = doneBtn;
}

///按钮点击
- (void)btnClick:(UIButton *)sender {
    NSString *title = sender.titleLabel.text;
    if ([title isEqualToString:@"Back"]) {
        if (self.didBottomBackBtnClickBlock) {
            self.didBottomBackBtnClickBlock();
        }
    } else if ([title isEqualToString:@"Cancel"]) {
        if (self.didBottomCancelBtnClickBlock) {
            self.didBottomCancelBtnClickBlock();
        }
    } else {
        if (self.didBottomDoneBtnClickBlock) {
            self.didBottomDoneBtnClickBlock();
        }
    }
}

- (void)hiddenDoneBtn {
    [UIView animateWithDuration:0.2 animations:^{
        CGRect btnFrame = self.backBtn.frame;
        btnFrame.origin.x = self.tz_width * 0.5 - 50;
        self.backBtn.frame = btnFrame;

        CGRect doneFrame = self.doneBtn.frame;
        doneFrame.origin.x = self.tz_width * 0.5 - 50;
        self.doneBtn.frame = doneFrame;
    } completion:^(BOOL finished) {
        self.doneBtn.hidden = true;
    }];
}

- (void)showDoneBtn {
    self.doneBtn.hidden = false;
    [UIView animateWithDuration:0.2 animations:^{
        CGRect backFrame = self.backBtn.frame;
        backFrame.origin.x = self.tz_width * 0.25 - 50;
        self.backBtn.frame = backFrame;

        CGRect doneFrame = self.doneBtn.frame;
        doneFrame.origin.x = self.tz_width * 0.75 - 50;
        self.doneBtn.frame = doneFrame;
    } completion:^(BOOL finished) {
        [self.backBtn setTitle:@"Cancel" forState:UIControlStateNormal];
    }];
}
@end
